package org.example.dao;

import org.example.entity.Air;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AirDao implements IAirDao {
    @Autowired
    private JdbcTemplate jdbcTemplatep;

    @Override
    public int add(Air air) {
        return jdbcTemplatep.update("insert into air(districtid,monitorTime,pm10,pm25,monitoringStation) values (?,?,?,?,?)",air.getDistrictid(),air.getMonitorTime(),air.getPm10(),air.getPm25(),air.getMonitoringStation());
    }

    @Override
    public int del(Long id) {
        return jdbcTemplatep.update("delete from air where id =?",id);
    }

    @Override
    public int update(Air air) {
        return jdbcTemplatep.update("update air set districtid=?, monitorTime=?, pm10=?, pm25=?, monitoringStation=? where id = ?",air.getDistrictid(),air.getMonitorTime(),air.getPm10(),air.getPm25(),air.getMonitoringStation(),air.getId());
    }

    @Override
    public Air get(Long id) {
        return jdbcTemplatep.queryForObject("select * from air where id = ?", new BeanPropertyRowMapper<>(Air.class), id);
    }

    @Override
    public List<Air> getAll() {
        return jdbcTemplatep.query("select * from air", new BeanPropertyRowMapper<>(Air.class));
    }
}
